<?php
class ControllerAccountReturn extends AccountController {
	
	public function index() {

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => US('account/return'),
        	'separator' => $this->_['text_separator']
      	);


		M('account/return');

		$page = $this->request->get('page', 1);

		$return_total = $this->model_account_return->getTotalReturns();
		$this->data['returns'] = $this->model_account_return->getReturns(($page - 1) * 10, 10);

		$this->data['pagination'] = $this->pagination($return_total, $page)->render();

		$this->display('account/return_list.tpl');
	}

	public function info() {

		$url = '';

		if (isset($this->request->get['page'])) {
			$url .= 'page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->_['heading_title'],
			'href'      => US('account/return', $url),
			'separator' => $this->_['text_separator']
		);

		$return_id = $this->request->get('return_id', 0);

		M('account/return');

		$return_info = $this->model_account_return->getReturn($return_id);

		if ($return_info) {

			$this->data['return_id'] = $return_info['return_id'];
			$this->data['order_id'] = $return_info['order_id'];
			$this->data['date_ordered'] = v5_date_format($return_info['date_ordered']);
			$this->data['date_added'] = v5_date_format($return_info['date_added']);
			$this->data['firstname'] = $return_info['firstname'];
			$this->data['lastname'] = $return_info['lastname'];
			$this->data['email'] = $return_info['email'];
			$this->data['telephone'] = $return_info['telephone'];
			$this->data['product'] = $return_info['product'];
			$this->data['model'] = $return_info['model'];
			$this->data['quantity'] = $return_info['quantity'];
			$this->data['reason'] = $return_info['reason'];
			$this->data['opened'] = $return_info['opened'] ? $this->_['text_yes'] : $this->_['text_no'];
			$this->data['comment'] = nl2br($return_info['comment']);
			$this->data['action'] = $return_info['action'];

			$this->data['histories'] = array();

			$results = $this->model_account_return->getReturnHistories($this->request->get['return_id']);

      		foreach ($results as $result) {
        		$this->data['histories'][] = array(
          			'date_added' => v5_date_format($result['date_added']),
          			'status'     => $result['status'],
          			'comment'    => nl2br($result['comment'])
        		);
      		}

			$this->data['continue'] = US('account/return', $url);

			$this->display('account/return_info.tpl');
		} else {

			$this->data['heading_title'] = $this->_['text_return'];
			$this->data['text_error'] = $this->_['text_error'];
			$this->data['button_continue'] = $this->_['button_continue'];

			$this->data['continue'] = US('account/return', $url);

			$this->display('error/not_found.tpl');
		}
	}

	public function insert() {
		
		M('account/return');

    	if ($this->request->isPost() && $this->validate()) {
			$this->model_account_return->addReturn($this->request->post);
			$this->redirect(US('account/return/success'));
    	}

		$this->document->addScript('static/javascript/jquery/jquery.validate.js');

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => US('account/return/insert'),
        	'separator' => $this->_['text_separator']
      	);

		M('account/order');

		if (isset($this->request->get['order_id'])) {
			$order_info = $this->model_account_order->getOrder($this->request->get['order_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $order_info;
			}
		}

		M('catalog/product');

		if (isset($this->request->get['product_id'])) {
			$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
		}

		$this->data['firstname'] = $this->vo->get('firstname', $this->customer->getFirstName());
		$this->data['lastname'] = $this->vo->get('lastname', $this->customer->getLastName());
		$this->data['email'] = $this->vo->get('email', $this->customer->getEmail());
		$this->data['telephone'] = $this->vo->get('telephone', $this->customer->getTelephone());
		
		if (isset($this->request->post['product'])) {
    		$this->data['product'] = $this->request->post['product'];
		} elseif (!empty($product_info)) {
			$this->data['product'] = $product_info['name'];
		} else {
			$this->data['product'] = '';
		}

		if (isset($this->request->post['model'])) {
    		$this->data['model'] = $this->request->post['model'];
		} elseif (!empty($product_info)) {
			$this->data['model'] = $product_info['model'];
		} else {
			$this->data['model'] = '';
		}
		
		$this->data['quantity'] = P('quantity', 1);
		$this->data['opened'] = P('opened', false);
		$this->data['return_reason_id'] = P('return_reason_id');
		
		M('localisation/return_reason');
    	$this->data['return_reasons'] = $this->model_localisation_return_reason->getReturnReasons();

   		$this->data['comment'] = P('comment');
		$this->data['captcha'] = P('captcha');
		
		if (C('config_return_id')) {
			M('catalog/page');
			
			$page_info = $this->model_catalog_page->getPage(C('config_return_id'));
			
			if ($page_info) {
				$this->data['text_agree'] = sprintf($this->_['text_agree'], U('page/index/info', 'page_id=' . C('config_return_id')), $page_info['title'], $page_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}
		
		$this->data['agree'] = P('agree', false);

		$this->display('account/return_form.tpl');
  	}

  	public function success() {

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => US('account/return'),
        	'separator' => $this->_['text_separator']
      	);

    	$this->data['heading_title'] = $this->_['heading_title'];
    	$this->data['text_message'] = $this->_['text_message'];
    	$this->data['continue'] = US('account/return');

 		$this->display('common/success.tpl');
	}

  	protected function validate() {
		if (C('config_return_id')) {
			M('catalog/page');
			
			$page_info = $this->model_catalog_page->getPage(C('config_return_id'));
			
			if ($page_info && !isset($this->request->post['agree'])) {
      			$this->setMessage('error_warning', sprintf($this->_['error_agree'], $page_info['title']));
				return false;
			}
		}
		
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('order_id', '', 'required', $this->_['error_order_id']);
    	
		$this->form_validation->set_rules('firstname', '', 'required|range_length[1,32]', $this->_['error_firstname']);
		
		$this->form_validation->set_rules('lastname', '', 'required|range_length[1,32]', $this->_['error_lastname']);

    	$this->form_validation->set_rules('email', '', 'required|range_length[5,96]|email', $this->_['error_email']);


    	$this->form_validation->set_rules('telephone', '', 'required|range_length[3,32]', $this->_['error_telephone']);

		$this->form_validation->set_rules('product', '', 'required|range_length[1,255]', $this->_['error_product']);

		$this->form_validation->set_rules('model', '', 'required|range_length[1,64]', $this->_['error_model']);

		$this->form_validation->set_rules('return_reason_id', '', 'required', $this->_['error_reason']);

		$this->form_validation->set_rules('captcha', '', 'required|equals['.$this->session->data['captcha'].']', $this->_['error_captcha']);
		
		return $this->form_validation->run();
  	}

	public function captcha() {
		$this->load->library('captcha');
		$captcha = new Captcha();
		$this->session->data['captcha'] = $captcha->getCode();
		$captcha->showImage();
	}
}
?>
